package mytask.view;

import java.util.HashMap;
import java.util.Map;

import mytask.domain.AuthManager;
import mytask.domain.TranslationManager;

import org.springframework.web.servlet.ModelAndView;

public final class ScreenManager {

    public static final String REDIRECT_HOME = "redirect:/";

    public static final String PAGE_TITLE = "pageTitle";

    public static final String PAGE_MENU = "pageMenu";

    public static final String LOCATION_MENU = "locationMenu";

    public static final String SCREEN_TITLE = "screenTitle";

    private ScreenManager() {
    }

    public static String getViewName(final Screen screenId) {
        switch (screenId) {
        case HOME_GUEST:
            return "home-guest";
        case HOME_USER:
            return "home-user";
        case FAQ:
            return "faq";
        case REGISTER:
            return "register";

        case PROJECTS_HOME:
            return "projects-home";
        case PROJECT_VIEW:
            return "project-view";
        case PROJECT_ADD:
            return "project-edit";
        case PROJECT_EDIT:
            return "project-edit";

        case TICKETS_HOME:
            return "tickets-home";
        case TICKET_VIEW:
            return "ticket-view";
        case TICKET_ADD:
            return "ticket-edit";
        case TICKET_EDIT:
            return "ticket-edit";
        case TICKET_LIST:
            return "ticket-list";

        case LOGIN:
            return "login";
        case LOGOUT:
            return "logout";

        case MESSAGE_ERROR:
            return "message-error";

        default:
            return "";
        }
    }

    public static ModelAndView get(final Screen screenId) {
        return get(screenId, new HashMap<String, Object>());
    }

    public static ModelAndView get(final Screen screenId, final Map<String, Object> params) {
        final ModelAndView modelAndView = new ModelAndView();
        final String viewName = getViewName(screenId);

        modelAndView.setViewName(viewName);

        modelAndView.addObject(PAGE_TITLE, getPageTitle("title." + viewName));
        modelAndView.addObject(SCREEN_TITLE, TranslationManager.get("title." + viewName));

        if (AuthManager.isUserLoggedIn()) {
            modelAndView.addObject(PAGE_MENU, getMainMenuForUser().build());
        } else {
            modelAndView.addObject(PAGE_MENU, getMainMenuForGuest().build());
        }

        modelAndView.addObject(LOCATION_MENU, getLocationMenuForScreen(screenId, viewName, params).build());

        return modelAndView;
    }

    public static ModelAndView getErrorMessage(final String message) {
        final ModelAndView screen = ScreenManager.get(Screen.MESSAGE_ERROR);
        screen.addObject("message", message);

        return screen;
    }

    private static MenuBuilder getMainMenuForGuest() {
        final MenuBuilder mb = new MenuBuilder();

        mb.addItem("/", TranslationManager.get("menu." + getViewName(Screen.HOME_GUEST)));
        mb.addItem("/register", TranslationManager.get("menu." + getViewName(Screen.REGISTER)));
        mb.addItem("/faq", TranslationManager.get("menu." + getViewName(Screen.FAQ)));

        return mb;
    }

    private static MenuBuilder getMainMenuForUser() {
        final MenuBuilder mb = new MenuBuilder();

        mb.addItem("/", TranslationManager.get("menu." + getViewName(Screen.HOME_GUEST)));
        mb.addItem("/projects", TranslationManager.get("menu." + getViewName(Screen.PROJECTS_HOME)));
        mb.addItem("/logout", TranslationManager.get("menu." + getViewName(Screen.LOGOUT)));
        mb.addItem("/faq", TranslationManager.get("menu." + getViewName(Screen.FAQ)));

        return mb;
    }

    private static MenuBuilder getLocationMenuForScreen(final Screen screenId, final String viewName,
            final Map<String, Object> params) {

        final MenuBuilder mb = new MenuBuilder();
        mb.addItem("/", TranslationManager.get("menu." + getViewName(Screen.HOME_GUEST)));

        switch (screenId) {
        case FAQ:
            mb.addItem("/faq", TranslationManager.get("title." + viewName));
            break;

        case REGISTER:
            mb.addItem("/register", TranslationManager.get("title." + viewName));
            break;

        case PROJECTS_HOME:
            mb.addItem("/projects", TranslationManager.get("title.projects-home"));
            break;

        case PROJECT_ADD:
            mb.addItem("/projects", TranslationManager.get("title.projects-home"));
            mb.addItem("/projects/add", TranslationManager.get("title.project-add"));
            break;

        case PROJECT_VIEW:
            mb.addItem("/projects", TranslationManager.get("title.projects-home"));
            mb.addItem("/projects/view?id=" + params.get("project-id"), (String) params.get("project-title"));
            break;

        case PROJECT_EDIT:
            mb.addItem("/projects", TranslationManager.get("title.projects-home"));
            mb.addItem("/projects/view?id=" + params.get("project-id"), (String) params.get("project-title"));
            mb.addItem("/projects/edit?id=" + params.get("project-id"), TranslationManager.get("title.project-edit"));
            break;

        case TICKET_ADD:
            mb.addItem("/projects", TranslationManager.get("title.projects-home"));
            mb.addItem("/projects/view?id=" + params.get("project-id"), (String) params.get("project-title"));
            mb.addItem("", TranslationManager.get("title.ticket-add"));
            break;
            
        case TICKET_VIEW:
            mb.addItem("/projects", TranslationManager.get("title.projects-home"));
            mb.addItem("/projects/view?id=" + params.get("project-id"), (String) params.get("project-title"));
            mb.addItem("/tickets/view?id=" + params.get("ticket-id"), (String) params.get("ticket-title"));
            break;

        case TICKET_EDIT:
            mb.addItem("/projects", TranslationManager.get("title.projects-home"));
            mb.addItem("/projects/view?id=" + params.get("project-id"), (String) params.get("project-title"));
            mb.addItem("/tickets/view?id=" + params.get("ticket-id"), (String) params.get("ticket-title"));
            mb.addItem("/tickets/edit?id=" + params.get("ticket-id"), TranslationManager.get("title.ticket-edit"));
            break;

        }

        return mb;
    }

    static String getPageTitle(final String key) {
        return TranslationManager.get(key) + " - " + TranslationManager.get("title.application");
    }
}
